#coding:utf-8
'''
Created on 2013年7月26日

@author: River
'''

import urllib2
from dagou.Agent import Agent

class JD(Agent):
    '''
   自动 分析京东的物品页面 
    '''

    def __init__(self):
        '''
        
        '''
        Agent.__init__(self)
        self.agentId = 1
        self.baseUrl = "http://search.jd.com/Search?"
        self.code = 'gbk'
        self.site = '京东'
        self.ename = 'jd'
#         self.ItemInfo = {'all' : "//li[@sku]",
#                          'store' : None,
#                          'name' : ("div[2]/a[1]", None),
#                          'price' : ("div[3]/strong[1]", 'class'),
#                          'link' : ("div[2]/a[1]", 'href'),
#                          'carriage' : None,
#                          'volume' : ("div[4]/a[1]", None),
#                          'end': (('price', 'json', 'self.ProcJson', 'http://p.3.cn/prices/mgets?skuIds='), 
# #                          'end': (('price', 'reqall', ('http://p.3.cn/prices/mgets?skuIds=', r'''"id":"(.*?)","p":"([0-9.]+)"''')),                                  
#                                  ('price', 'fun', 'self.GetNumFromStr'),
#                                  ('carriage', 'fun', 'self.GetNumFromStr'),
#                                  ('volume', 'fun', 'self.GetNumFromStr')),
#                          'fliter' : "price < 0.5 * oldprice",
#                          }
    
    def ProcJson(self, url):
        # list :[{u'p': u'1888.00', u'm': u'2699.00', u'id': u'J_959495'}, 
        jsondata = Agent.ProcJson(self, url)
        return dict((a['id'], a['p']) for a in jsondata)
            
    def GetSearchList(self):
        while True:
            pri,form = self.que.get(1)
            self.BeginProcForm(form)
            print "seraching ",form.name
            url = 'keyword=%s&enc=utf-8&area=' %form.name
            url = self.baseUrl + url
            req = urllib2.Request(url)
            result = self.opener.open(req).read()
#             open('jd.html','w').write(result)
    #         url2 = "http://price.360buy.com/PromotionFlag.aspx?pid=931716,931710,917460,919670,917461,919669,933093,933094,934434,934438,910152,910751,910162,1025667505,910273,1026551881,1026433519,910154,1025667506,910158,928034,923970,1027169314,910155,910749,916512,923968,910270,1026909436,923967,1026551880,1026433520,923966,923977,910269,933249"
    #         req = urllib2.Request(url2)
    #         req.add_header("Referer",url)
    #         dd = self.opener.open(req).read()
    #         open('dd.html','w').write(dd)
            self.ParseResult(result, form, pri)
            self.EndProcForm(form)
        
    def SendRequest(self, itmstr):
        '''
        获取物品列表
        :param itmstr:
        '''
    
#         postdata=urllib.urlencode({'keyword': itmstr.encode('GBK'),
#                                    'enc':'utf-8',
#                                    'area':'22',
#         })
         
        req = urllib2.Request(
            url='http://search.jd.com/Search?keyword=%s&enc=utf-8&area=22' %itmstr
#             data=postdata
        )        
        
        # 提交搜索表单 
        Agent.logger.debug('...login form submitted')  # @UndefinedVariable
        result = self.opener.open(req).read()
        Agent.logger.debug('...login succeed!')  # @UndefinedVariable
        open('jd.html','w').write(result)


if __name__ == '__main__':
    jd = JD()